import tkinter as tk
import math

high = 0.2  # 默认0.2层高

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        frame = tk.Frame(self)
        frame.pack(padx=400, pady=300)

        # 标签和输入框
        self.label_fluency = tk.Label(frame, text="流量大小mm**3:")
        self.label_fluency.pack(side="top")
        self.entry_fluency = tk.Entry(frame)
        self.entry_fluency.pack(side="top")

        self.label_nozzle = tk.Label(frame, text="喷嘴直径mm:")
        self.label_nozzle.pack(side="top")
        self.entry_nozzle = tk.Entry(frame)
        self.entry_nozzle.pack(side="top")

        self.label_speed = tk.Label(frame, text="速度mm/s:")
        self.label_speed.pack(side="top")
        self.entry_speed = tk.Entry(frame)
        self.entry_speed.pack(side="top")

        # 计算按钮
        self.calculate_button = tk.Button(frame, text="计算", command=self.calculate)
        self.calculate_button.pack(side="top")

        # 输出结果标签
        self.result_label = tk.Label(frame, text="")
        self.result_label.pack(side="top")

    def calculate(self):
        try:
            fluency = self.entry_fluency.get()
            nozzle = self.entry_nozzle.get()
            speed = self.entry_speed.get()

            if fluency and nozzle:
                # 当输入流量大小和喷嘴直径时计算速度
                fluency = float(fluency)
                nozzle = float(nozzle)
                result_speed = fluency / (nozzle*high)  # 示例计算公式
                self.result_label.config(text=f"计算结果 - 速度: {result_speed:.2f}")
                self.entry_speed.delete(0, tk.END)  # 清空速度输入框

            elif fluency and speed:
                # 当输入流量大小和速度时计算喷嘴直径
                fluency = float(fluency)
                speed = float(speed)
                result_nozzle = fluency / (speed*high)  # 示例计算公式
                self.result_label.config(text=f"计算结果 - 喷嘴直径: {result_nozzle:.2f}")
                self.entry_nozzle.delete(0, tk.END)  # 清空喷嘴直径输入框

            elif nozzle and speed:
                # 当输入喷嘴直径和速度时计算流量大小
                nozzle = float(nozzle)
                speed = float(speed)
                result_fluency = nozzle*high*speed  # 示例计算公式
                self.result_label.config(text=f"计算结果 - 流量大小: {result_fluency:.2f}")
                self.entry_fluency.delete(0, tk.END)  # 清空流量大小输入框

            else:
                self.result_label.config(text="请至少输入两个有效的数值！")

        except ValueError:
            self.result_label.config(text="请输入有效的数字！")

if __name__ == '__main__':
    root = tk.Tk()
    app = Application(master=root)
    app.master.title("闲着没事写的耗材流量速度计算默认0.2层高")
    app.mainloop()
